<template>
  <div class="register-container">
    <div class="register-form">
      <h2>新用户注册</h2>
      <el-form
        :model="formData"
        :rules="formRules"
        ref="ruleFormRef"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="formData.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            type="password"
            v-model="formData.confirmPassword"
          ></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="formData.nickname"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register(ruleFormRef)"
            >注册</el-button
          >
          <el-button type="primary" @click="resetForm(ruleFormRef)"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <div class="login-link">
        已有账号？<router-link to="/login">去登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { registerUser } from '@/api/user'
import { useRouter } from 'vue-router'
import { usePageTitle } from '@/hooks/usePageTitle'
import { userNavtoHome } from '@/hooks/userNavTopHome'

usePageTitle('注册')
userNavtoHome()

interface RuleForm {
  username: string
  password: string
  confirmPassword: string
  nickname: string
}

const router = useRouter()
const formData = reactive<RuleForm>({
  username: 'admin',
  password: '123456',
  confirmPassword: '123456',
  nickname: ''
})

const ruleFormRef = ref<FormInstance>()

const formRules = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9_]+$/,
      message: '用户名只能包含字母、数字和下划线',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 10, message: '密码长度在5-10个字符之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (_rule: any, value: any, callback: any) => {
        if (value !== formData.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  nickname: [{ message: '请输入昵称', trigger: 'blur' }]
})

const register = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      console.log(formData)
      await registerUser(formData)
      ElMessage.success('注册成功')
      // 重置表单
      formEl.resetFields()
      router.push('/login')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  formEl?.resetFields()
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 80vh;
}

.register-form {
  width: 400px;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.login-link {
  margin-top: 10px;
  text-align: center;
}

.login-link a {
  color: #409eff;
  cursor: pointer;
}
</style>
